package cn.fws.meap.utils;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;

import cn.fws.meap.system.constants.QiNiuParam;

/**
 * 七牛云工具
 * 
 * @author chenkh
 * @date 2019-03-20
 */
public class QiNiuUtil {

	/**
	 * 七牛云机房对象
	 * @param ECN 华东
	 * @param NCN 华北
	 * @param SCN 华南
	 * @param NA 北美
	 * @param ASG 东南亚
	 */
	public static Zone ECN = Zone.zone0();
	public static Zone NCN = Zone.zone1();
	public static Zone SCN = Zone.zone2();
	public static Zone NA = Zone.zoneNa0();
	public static Zone ASG = Zone.zoneAs0();
	
	/**
	 * 获取七牛普通上传凭证
	 * @param bucketName 存储空间名称
	 * @return uploadToken 上传凭证
	 */
	public static String getUploadToken(String bucketName){
		Auth auth = Auth.create(QiNiuParam.ACCESS_KEY, QiNiuParam.SECRET_KEY);
		String upToken = auth.uploadToken(bucketName);
		return upToken;
	}
	
	/**
	 * 获取七牛覆盖上传凭证
	 * @param bucketName 存储空间名称
	 * @param fileName 文件名
	 * @return uploadToken 上传凭证
	 */
	public static String getUploadToken(String bucketName, String fileName){
		Auth auth = Auth.create(QiNiuParam.ACCESS_KEY, QiNiuParam.SECRET_KEY);
		String upToken = auth.uploadToken(bucketName, fileName);
		return upToken;
	}
	
	/**
	 * 获取七牛自定义返回值的上传凭证
	 * @param bucketName 存储空间名称
	 * @return uploadToken 上传凭证
	 */
	public static String getUploadToken(String bucketName, Map<String, String> returnBody){
		StringMap putPolicy = new StringMap();
		String policy = "{";
		for (Map.Entry<String,String> entry : returnBody.entrySet()) {
			policy += "\""+entry.getKey()+"\":"+entry.getValue()+",";
		}
		policy = policy.substring(0,policy.length()-1)+"}";
		putPolicy.put("returnBody", policy);
		long expireSeconds = 3600;
		Auth auth = Auth.create(QiNiuParam.ACCESS_KEY, QiNiuParam.SECRET_KEY);
		String upToken = auth.uploadToken(bucketName, null, expireSeconds, putPolicy);
		return upToken;
	}
	
	/**
	 * 将文本信息作为html文件上传服务器
	 * @param bucketName 存储空间名称
	 * @param fileMsg 文本信息
	 * @return fileUrl 文件地址
	 */
	public static String uploadFile(String bucketName, String fileMsg){
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(SCN);
		UploadManager uploadManager = new UploadManager(cfg);
		byte[] uploadBytes = null;
		try {
			uploadBytes = fileMsg.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String uploadToken = getUploadToken(bucketName);
		try {
		    Response response = uploadManager.put(uploadBytes, null, uploadToken, null, "text/html", true);
		    //解析上传成功的结果
		    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    
		    //拼接文件地址
		    String fileUrl = "";
		    switch (bucketName) {
				case QiNiuParam.MEAP_IMG:
					fileUrl = QiNiuParam.IMG_URL + putRet.key;
					break;
				case QiNiuParam.MEAP_DOCUMENT:
					fileUrl = QiNiuParam.DOCUMENT_URL + putRet.key;
					break;
				case QiNiuParam.MEAP_VIDEO:
					fileUrl = QiNiuParam.VIDEO_URL + putRet.key;
					break;
				case QiNiuParam.MEAP_AUDIO:
					fileUrl = QiNiuParam.AUDIO_URL + putRet.key;
					break;
				default:
					break;
			}
		    return fileUrl;
		} catch (QiniuException ex) {
			return null;
		}
	}
	
	/**
	 * 获取七牛url中的文件内容
	 * @param fileUrl 文件地址
	 * @return fileMsg 文件信息
	 */
	public static String getFileMsg(String fileUrl){
		String doGet = HttpUtil.doGet(fileUrl, null);
		System.out.println("["+doGet+"]");
		String msg = "";
		try {
			msg = new String(doGet.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return msg;
	}

}
